package com.wiloon.java8x.thread.exchanger;

/**
 * Created by wiloon on 6/2/17;
 */

import java.util.concurrent.Exchanger;
import java.util.concurrent.atomic.AtomicReference;

public class ThreadB implements Runnable {

    private Exchanger<Integer> exchanger;

    private final AtomicReference<Integer> last = new AtomicReference<Integer>(
            10);

    public ThreadB(Exchanger<Integer> exchanger) {
        this.exchanger = exchanger;
    }

    public void run() {
        try {
            while (true) {
                last.set(exchanger.exchange(last.get()));
                System.out.println(" After calling exchange. Thread B has value: " + last.get());
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}